<?php
namespace Bshop\Controller;
class CheckController extends CkadminController {
    public function index(){
    	$this->display();
    }
    /**
     * 验票
     */
    public function checkcode(){
    	if(IS_AJAX){
    		$code = I('post.code');
    		$type = (int)I('post.type');
    		if($code){
    			$db = M('order_ticket as ot');
    			$re = $db->join("left join ".C('DB_PREFIX')."order as o on o.id = ot.order_id left join ".C('DB_PREFIX')."activity_ticket as at on at.id = ot.ticket_id left join ".C('DB_PREFIX')."activity as a on a.id = o.aid left join ".C('DB_PREFIX')."user as u on o.uid = u.id")->where("ot.ticket_no='$code' and o.pay_status !='0'  and ot.shop_id = '{$_SESSION['eventor_shop_user_id']}'")->field("ot.*,a.name,at.ticket_name,u.username")->find();
    			if(!empty($re)){
    				if($re['is_use'] == 0 || $re['is_use'] == 4){
    					/* $a = \Org\Web\activity::ticket_is_out_date($re['order_id'],$re['ticket_no']);
    					if($a){
    						echo jsonencode(300,'此票已过期啦');exit;
    					}else{ */
    					if($type == 2){
    						$data['id'] = $re['id'];
    						$data['check_time'] = date("Y-m-d H:i:s");
    						$data['is_use'] = 1;
    						$res = $db->save($data);
    						if($res!=false){
    							//改变订单状态
    							$ree = $db->where("order_id='{$re['order_id']}' and is_use = '0' ")->find();
    							if(empty($ree)){
    								M('order')->where("id='{$re['order_id']}'")->setField('pay_status',6);
    							}
    							//发短信
    							$content = "【可能科技】您购买的[".$re['name']."]下的票号[".$re['ticket_no']."]已使用~";
    							\Org\Oa\msg::sendmessage($re['username'],'',$content);
    							echo jsonencode(200);
    						}else{
    							echo jsonencode(300,'验票失败，请重新运行');
    						}
    					}else{
    						echo jsonencode(200,'',$this->html($re));
    					}
    					/* } */
    				}else{
    					echo jsonencode(200,'',$this->html($re));
    				}
    			}else{
    				echo jsonencode(300,'票号输入不正确');
    			}
    		}else{
    			echo jsonencode(300,'请输入票号');
    		}
    	}
    }
    private function html($re){
    	if($re['is_use'] == 2 || $re['is_use'] == 3){
    		$list = M('user_refund')->where("ticket_id ='{$re['ticket_id']}' and shop_id='{$re['shop_id']}' and order_id='{$re['order_id']}'")->field("check_time,add_time")->find();
    	}
    	$qu = "";
    	$piao = "";
    	if($re['is_use'] == 0){
    		$piao = '<h3 class="piao-status success">可验票</h3>';
    	}elseif ($re['is_use'] == 4){
    		$piao = '<h3 class="piao-status past">已过期</h3>';
    	}elseif ($re['is_use'] == 1){
    		$piao = '<h3 class="piao-status red">已验票</h3>';
    	}elseif ($re['is_use'] == 2){
    		$piao = '<h3 class="piao-status past">已申请退款</h3>';
    		$qu  = '<td>申请时间</td>
				    <td>'.$list['add_time'].'</td>';
    	}elseif ($re['is_use'] == 3){
    		$piao = '<h3 class="piao-status red">已退款</h3>';
    		$qu  = '<td>退款时间</td>
				    <td>'.$list['check_time'].'</td>';
    	}
    	$html=$piao.'
					<table class="table table-border">
						<tbody>
							<tr>
								<td>票号</td>
								<td>'.$re['ticket_no'].'</td>
								<td>票名</td>
								<td>'.$re['ticket_name'].'</td>
							</tr>
							<tr>
								<td>活动</td>
								<td>'.$re['name'].'</td>
								<td>场次时间</td>
								<td>'.$re['ticket_date'].'</td>
							</tr>
							<tr>
								<td>购票人</td>
								<td>'.$re['username'].'</td>
								'.$qu.'
							</tr>
						</tbody>
						</table>';
    	return $html;
    }
}